package com.zx.idc.backend.gui.slog.service;

import com.zx.idc.backend.gui.api.management.infosecurity.activeresources.service.ActiveResourceService;
import com.zx.idc.cache.entity.HotDomainMetaInfo;
import com.zx.idc.cache.slog.SlogHotDomainCacheService;
import com.zx.idc.common.lang.ListUtil;
import com.zx.idc.common.util.LocalDateTimeFormatUtil;
import com.zx.idc.ds.slog.vo.SlogHotDomainVo;
import com.zx.idc.xml.entity.activeresources.ActiveResources;
import com.zx.idc.xml.entity.activeresources.finddomainlist.FindDomainList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = false, rollbackFor = Exception.class)
public class SlogHotDomainInterfaceService {

    @Autowired
    private ActiveResourceService activeResourceService;

    @Autowired
    private SlogHotDomainCacheService slogHotDomainCacheService;

    public Boolean reportData(List<SlogHotDomainVo> slogHotDomainVos) {
        if (slogHotDomainVos == null || slogHotDomainVos.isEmpty()) {
            return true;
        }

        //组装数据
        ActiveResources activeResources = getActiveResources(slogHotDomainVos);

        //调用接口上报
        if (ListUtil.isNotEmpty(activeResources.getFindDomainList())) {
            activeResourceService.reportData(activeResources);
        }
        return true;
    }

    private ActiveResources getActiveResources(List<SlogHotDomainVo> entityList) {
        return new ActiveResources()
            .setFindDomainList(
                entityList.stream()
                    .peek(uploadHotDomain -> {
                        HotDomainMetaInfo meta = slogHotDomainCacheService.getMeta(Long.valueOf(uploadHotDomain.getHouseId()), uploadHotDomain.getFindDomainListDomain());
                        if (Objects.nonNull(meta)) {
                            Optional.ofNullable(meta.getVisitCnt()).ifPresent(uploadHotDomain::setVisitsCount);
                            Optional.ofNullable(meta.getLstFoundTime()).ifPresent(lstFoundTime ->
                                uploadHotDomain.setFindDomainListLastTime(LocalDateTimeFormatUtil.formatLocalDateTime(lstFoundTime))
                            );
                        }
                    })
                    .map(obj ->
                        new FindDomainList()
                            .setFindDomainListDomain(obj.getFindDomainListDomain())
                            .setFindDomainListFirstTime(obj.getFindDomainListFirstTime())
                            .setFindDomainListLastTime(obj.getFindDomainListLastTime())
                            .setBlock(obj.getBlock())
                            .setVisitsCount(obj.getVisitsCount())
                            .setTopDomainflag(obj.getTopDomainflag())
                            .setTopDomain(obj.getTopDomain())
                            .setIp(obj.getIp())
                            .setHouseId(obj.getHouseId())
                            .setIdcId(obj.getIdcId())
                    ).collect(Collectors.toList())
            );
    }
}
